R Introduction

link to Software Carpentry

R Fundamentals

getwd();
## [1] "E:/Users/Admin/Google Drive/MiApp/kinito.seminar.under"
#setwd("~/Desktop");
ls();
## character(0)
#rm(list=ls());

R data structures

link to pycon

R variable

my_number <- 42;
print(my_number);
## [1] 42
chv1 <- "hello";
print(chv1);
## [1] "hello"
x <- 6+4;
print(x);
## [1] 10
y <- 4;
x / y;
## [1] 2.5

R vector

a <- c(1,2,5.3,6,-2,4); # numeric vector
b <- c("one","two","three"); # character vector
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE); #logical vector
v1 <- c(1, 5.5, 1e2);
v2 <- c(0.14, 0, -2);
v3 <- c(v1, v2);
v3[2];
## [1] 5.5
v3_sub <- v3[c(2,3)];
v1 %*% v2;
##         [,1]
## [1,] -199.86

R matrix

1:6;
## [1] 1 2 3 4 5 6
seq(from=1, to=12, by=4);
## [1] 1 5 9
ma <- matrix(1:6, nrow=3, ncol=2);
ma;
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
mb <- matrix(7:9, nrow=3, ncol=1);
mb;
##      [,1]
## [1,]    7
## [2,]    8
## [3,]    9
rbind(ma, c(100, 200, 300));
## Warning in rbind(ma, c(100, 200, 300)): number of columns of result is not
## a multiple of vector length (arg 2)
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
## [4,]  100  200
m <- cbind(ma, mb);
m;
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
ncol(m);
## [1] 3
nrow(m);
## [1] 3
dim(m);
## [1] 3 3
t(m);
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
diag(m);
## [1] 1 5 9
#mymatrix <- matrix(vector, nrow=r, ncol=c, byrow=FALSE, dimnames=list(char_vector_rownames, char_vector_colnames));
y<-matrix(1:20, nrow=5,ncol=4);
cells <- c(1,26,24,68);
rnames <- c("R1", "R2");
cnames <- c("C1", "C2");
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames));

R array or multidimensional matrix

#help(array);

R data frame

d <- c(1,2,3,4);
e <- c("red", "white", "red", NA);
f <- c(TRUE,TRUE,TRUE,FALSE);
mydata <- data.frame(d,e,f);
names(mydata) <- c("ID","Color","Passed");

R list-ordered collection of objects (components), possibly unrelated

# example a string, a numeric vector, a matrix, and a scalar
a <- c(1,2,5.3,6,-2,4);
y <- matrix(1:20, nrow=5,ncol=4);
w <- list(name="Fred", mynumbers=a, mymatrix=y, age=5.3);
# example of a list containing two lists
v <- c(w,list(1,2));

R factor-nominal values (vector of integers) mapped to these integers

# variable gender with 20 "male" entries and 30 "female" entries
gender <- c(rep("male",20), rep("female", 30));
gender <- factor(gender);
# stores gender as 20 1s and 30 2s and associates 1=female, 2=male internally (alphabetically)
# R now treats gender as a nominal variable
# variable rating coded as "large", "medium", "small"
rating <- ordered(factor("large", "medium", "small"));
# recodes rating to 1,2,3 and associates 1=large, 2=medium, 3=small internally R now treats rating as ordinal

R date

as.Date(NA);
## [1] NA
Sys.Date();
## [1] "2016-09-05"
date(); #datetime
## [1] "Mon Sep 05 13:32:21 2016"
#Format
#Convert
#help(as.Date)
#help(strftime)
#help(ISOdatetime)

link to Cyclismo Date

R NA, NULL

# NA for all
# NULL only objects

R goodies

#length(object);  # number of elements or components
#str(object);      # structure of an object
#class(object);   # class or type of an object
#names(object);   # names
#c(object,object,...);      # combine objects into a vector
#cbind(object, object, ...); # combine objects as columns
#rbind(object, object, ...); # combine objects as rows
#object;          # prints the object
#rm(object);     # delete an object
#newobject <- edit(object); # edit copy and save as newobject
#fix(object);    # edit in place

R data reorganization

link to pycon

R data import

link to R-Json

R data visualization

R data visualization base

link to pycon

R data visualization grammar of graphics

link to pycon

link to ggplot2

R software carpentry

link to Software Carpentry

R Stats for newbies

Stats for newbies

Stats for newbies

Rmarkdown Examples

link to Rmarkdown examples

R variance

R 1-Sample

link to One Sample

R 2-Samples

R 2-Plus-Samples